package com.metering.purchaseServerAdmin.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.metering.purchaseServerAdmin.dao.DeptDao;
import com.metering.purchaseServerAdmin.dao.UserDao;
import com.metering.purchaseServerAdmin.dao.UserRoleDao;
import com.metering.purchaseServerAdmin.domain.DeptDO;
import com.metering.purchaseServerAdmin.domain.Tree;
import com.metering.purchaseServerAdmin.domain.UserDO;
import com.metering.purchaseServerAdmin.domain.UserRoleDO;
import com.metering.purchaseServerAdmin.service.UserService;
import com.metering.purchaseServerAdmin.utils.MD5Utils;
import com.metering.purchaseServerAdmin.vo.UserVO;

@Transactional(rollbackFor = Exception.class)
public class UserServiceImpl implements UserService {
	@Autowired
	private UserDao userMapper;
	
	@Autowired
	private UserRoleDao userRoleMapper;
	
	@Autowired
	private DeptDao deptMapper;
	
	private static final Logger logger = LoggerFactory.getLogger(UserService.class);

	@Override
	public UserDO get(Long id) {
		// TODO Auto-generated method stub
		List<Long> roleIds = userRoleMapper.listRoleId(id);
		UserDO user = userMapper.get(id);
		user.setDeptName(deptMapper.get(user.getDeptId()).getName());
		user.setRoleIds(roleIds);
		return user;
	}

	@Override
	public List<UserDO> list(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return userMapper.list(map);
	}

	@Override
	public int count(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return userMapper.count(map);
	}

	@Override
	public int save(UserDO user) {
		// TODO Auto-generated method stub
		int count = userMapper.save(user);
		Long userId = user.getUserId();
		List<Long> roles = user.getRoleIds();
		userRoleMapper.removeByUserId(userId);
		List<UserRoleDO> list = new ArrayList<>();
		for (Long roleId : roles) {
			UserRoleDO ur = new UserRoleDO();
			ur.setUserId(userId);
			ur.setRoleId(roleId);
			list.add(ur);
		}
		if (list.size() > 0) {
			userRoleMapper.batchSave(list);
		}
		return count;
	}

	@Override
	public int update(UserDO user) {
		// TODO Auto-generated method stub
		int r = userMapper.update(user);
		Long userId = user.getUserId();
		List<Long> roles = user.getRoleIds();
		if(null!=roles){
			userRoleMapper.removeByUserId(userId);
			List<UserRoleDO> list = new ArrayList<>();
			for (Long roleId : roles) {
				UserRoleDO ur = new UserRoleDO();
				ur.setUserId(userId);
				ur.setRoleId(roleId);
				list.add(ur);
			}
			if (list.size() > 0) {
				userRoleMapper.batchSave(list);
			}
		}
		return r;
	}

	@Override
	public int remove(Long userId) {
		// TODO Auto-generated method stub
		userRoleMapper.removeByUserId(userId);
		return userMapper.remove(userId);
	}

	@Transactional
	@Override
	public int batchremove(Long[] userIds) {
		// TODO Auto-generated method stub
		int count = userMapper.batchRemove(userIds);
		userRoleMapper.batchRemoveByUserId(userIds);
		return count;
	}

	@Override
	public boolean exits(Map<String, Object> params) {
		// TODO Auto-generated method stub
		boolean exits = userMapper.list(params).size() > 0;
		return exits;
	}

	@Override
	public Set<String> listRoles(Long userId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int resetPwd(UserVO userVO, UserDO userDO) throws Exception {
		// TODO Auto-generated method stub
		if(Objects.equals(userVO.getUserDO().getUserId(),userDO.getUserId())){
			if(Objects.equals(MD5Utils.encrypt(userDO.getUsername(),userVO.getPwdOld()),userDO.getPassword())){
				userDO.setPassword(MD5Utils.encrypt(userDO.getUsername(),userVO.getPwdNew()));
				return userMapper.update(userDO);
			}else{
				throw new Exception("输入的旧密码有误！");
			}
		}else{
			throw new Exception("你修改的不是你登录的账号！");
		}
	}

	@Override
	public int adminResetPwd(UserVO userVO) throws Exception {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public Tree<DeptDO> getTree() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int updatePersonal(UserDO userDO) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public Map<String, Object> updatePersonalImg(MultipartFile file, String avatar_data, Long userId) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
